package cn.hyh;

/**
 * @author hyh
 * 2020/7/30
 */
public class StringUtil {

    /**
     * 将字符串转换为驼峰, 默认分隔符为-, 首字母不大写
     *
     * @param str 字符串
     * @return 驼峰格式字符串
     */
    public static String toCamelCase(String str) {
        return toCamelCase(str, '_', false);
    }

    public static String toCamelCase(String str, char splitStr) {
        return toCamelCase(str, splitStr, false);
    }

    /**
     * 将字符串转换为驼峰, 默认分隔符为-
     *
     * @param str      字符串
     * @param hasFirst 首字母是否大写
     * @return 驼峰格式字符串
     */
    public static String toCamelCase(String str, Boolean hasFirst) {
        return toCamelCase(str, '_', hasFirst);
    }

    /**
     * 将字符串转换为驼峰
     *
     * @param str      字符串
     * @param splitStr 用于分割每个单词的字符
     * @param hasFirst 首字母是否大写
     * @return 驼峰格式字符串
     */
    public static String toCamelCase(CharSequence str, char splitStr, Boolean hasFirst) {
        if (null == str) {
            return null;
        }
        String name2 = str.toString();
        if (name2.contains(String.valueOf(splitStr))) {
            final StringBuilder sb = new StringBuilder(name2.length());
            boolean upperCase = hasFirst;
            for (int i = 0; i < name2.length(); i++) {
                char c = name2.charAt(i);
                if (c == splitStr) {
                    upperCase = true;
                } else if (upperCase) {
                    sb.append(Character.toUpperCase(c));
                    upperCase = false;
                } else {
                    sb.append(Character.toLowerCase(c));
                }
            }
            return sb.toString();
        } else {
            return name2;
        }
    }
}
